
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Built-in Views &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Meta-documentation and miscellany" href="../misc/index.html" />
    <link rel="prev" title="Validators" href="validators.html" />



 
<script type="text/javascript" src="../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="validators.html" title="Validators">previous</a>
     |
    <a href="index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="../misc/index.html" title="Meta-documentation and miscellany">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-views">
            
  <div class="section" id="s-module-django.views">
<span id="s-built-in-views"></span><span id="module-django.views"></span><span id="built-in-views"></span><h1>Built-in Views<a class="headerlink" href="#module-django.views" title="Permalink to this headline">¶</a></h1>
<p>Several of Django’s built-in views are documented in
<a class="reference internal" href="../topics/http/views.html"><span class="doc">Writing views</span></a> as well as elsewhere in the documentation.</p>
<div class="section" id="s-serving-files-in-development">
<span id="serving-files-in-development"></span><h2>Serving files in development<a class="headerlink" href="#serving-files-in-development" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="django.views.static.serve">
<code class="descclassname">static.</code><code class="descname">serve</code>(<em>request</em>, <em>path</em>, <em>document_root</em>, <em>show_indexes=False</em>)<a class="headerlink" href="#django.views.static.serve" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>There may be files other than your project’s static assets that, for
convenience, you’d like to have Django serve for you in local development.
The <a class="reference internal" href="#django.views.static.serve" title="django.views.static.serve"><code class="xref py py-func docutils literal notranslate"><span class="pre">serve()</span></code></a> view can be used to serve any directory
you give it. (This view is <strong>not</strong> hardened for production use and should be
used only as a development aid; you should serve these files in production
using a real front-end web server).</p>
<p>The most likely example is user-uploaded content in <a class="reference internal" href="settings.html#std:setting-MEDIA_ROOT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MEDIA_ROOT</span></code></a>.
<code class="docutils literal notranslate"><span class="pre">django.contrib.staticfiles</span></code> is intended for static assets and has no
built-in handling for user-uploaded files, but you can have Django serve your
<a class="reference internal" href="settings.html#std:setting-MEDIA_ROOT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MEDIA_ROOT</span></code></a> by appending something like this to your URLconf:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.views.static</span> <span class="k">import</span> <span class="n">serve</span>

<span class="c1"># ... the rest of your URLconf goes here ...</span>

<span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">:</span>
    <span class="n">urlpatterns</span> <span class="o">+=</span> <span class="p">[</span>
        <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^media/(?P&lt;path&gt;.*)$&#39;</span><span class="p">,</span> <span class="n">serve</span><span class="p">,</span> <span class="p">{</span>
            <span class="s1">&#39;document_root&#39;</span><span class="p">:</span> <span class="n">settings</span><span class="o">.</span><span class="n">MEDIA_ROOT</span><span class="p">,</span>
        <span class="p">}),</span>
    <span class="p">]</span>
</pre></div>
</div>
<p>Note, the snippet assumes your <a class="reference internal" href="settings.html#std:setting-MEDIA_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MEDIA_URL</span></code></a> has a value of
<code class="docutils literal notranslate"><span class="pre">'/media/'</span></code>. This will call the <a class="reference internal" href="#django.views.static.serve" title="django.views.static.serve"><code class="xref py py-func docutils literal notranslate"><span class="pre">serve()</span></code></a> view,
passing in the path from the URLconf and the (required) <code class="docutils literal notranslate"><span class="pre">document_root</span></code>
parameter.</p>
<p>Since it can become a bit cumbersome to define this URL pattern, Django
ships with a small URL helper function <a class="reference internal" href="urls.html#django.conf.urls.static.static" title="django.conf.urls.static.static"><code class="xref py py-func docutils literal notranslate"><span class="pre">static()</span></code></a>
that takes as parameters the prefix such as <a class="reference internal" href="settings.html#std:setting-MEDIA_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MEDIA_URL</span></code></a> and a dotted
path to a view, such as <code class="docutils literal notranslate"><span class="pre">'django.views.static.serve'</span></code>. Any other function
parameter will be transparently passed to the view.</p>
</div>
<div class="section" id="s-error-views">
<span id="s-id1"></span><span id="error-views"></span><span id="id1"></span><h2>Error views<a class="headerlink" href="#error-views" title="Permalink to this headline">¶</a></h2>
<p>Django comes with a few views by default for handling HTTP errors. To override
these with your own custom views, see <a class="reference internal" href="../topics/http/views.html#customizing-error-views"><span class="std std-ref">Customizing error views</span></a>.</p>
<div class="section" id="s-the-404-page-not-found-view">
<span id="s-http-not-found-view"></span><span id="the-404-page-not-found-view"></span><span id="http-not-found-view"></span><h3>The 404 (page not found) view<a class="headerlink" href="#the-404-page-not-found-view" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="django.views.defaults.page_not_found">
<code class="descclassname">defaults.</code><code class="descname">page_not_found</code>(<em>request</em>, <em>exception</em>, <em>template_name='404.html'</em>)<a class="headerlink" href="#django.views.defaults.page_not_found" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>When you raise <a class="reference internal" href="../topics/http/views.html#django.http.Http404" title="django.http.Http404"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Http404</span></code></a> from within a view, Django loads a
special view devoted to handling 404 errors. By default, it’s the view
<a class="reference internal" href="#django.views.defaults.page_not_found" title="django.views.defaults.page_not_found"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.views.defaults.page_not_found()</span></code></a>, which either produces a very
simple “Not Found” message or loads and renders the template <code class="docutils literal notranslate"><span class="pre">404.html</span></code> if
you created it in your root template directory.</p>
<p>The default 404 view will pass two variables to the template: <code class="docutils literal notranslate"><span class="pre">request_path</span></code>,
which is the URL that resulted in the error, and <code class="docutils literal notranslate"><span class="pre">exception</span></code>, which is a
useful representation of the exception that triggered the view (e.g. containing
any message passed to a specific <code class="docutils literal notranslate"><span class="pre">Http404</span></code> instance).</p>
<p>Three things to note about 404 views:</p>
<ul class="simple">
<li>The 404 view is also called if Django doesn’t find a match after
checking every regular expression in the URLconf.</li>
<li>The 404 view is passed a <a class="reference internal" href="templates/api.html#django.template.RequestContext" title="django.template.RequestContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">RequestContext</span></code></a> and
will have access to variables supplied by your template context
processors (e.g. <code class="docutils literal notranslate"><span class="pre">MEDIA_URL</span></code>).</li>
<li>If <a class="reference internal" href="settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> is set to <code class="docutils literal notranslate"><span class="pre">True</span></code> (in your settings module), then
your 404 view will never be used, and your URLconf will be displayed
instead, with some debug information.</li>
</ul>
<div class="versionchanged">
<span class="title">Changed in Django 1.10:</span> <p>Passing a nonexistent <code class="docutils literal notranslate"><span class="pre">template_name</span></code> will raise <code class="docutils literal notranslate"><span class="pre">TemplateDoesNotExist</span></code>.</p>
</div>
</div>
<div class="section" id="s-the-500-server-error-view">
<span id="s-http-internal-server-error-view"></span><span id="the-500-server-error-view"></span><span id="http-internal-server-error-view"></span><h3>The 500 (server error) view<a class="headerlink" href="#the-500-server-error-view" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="django.views.defaults.server_error">
<code class="descclassname">defaults.</code><code class="descname">server_error</code>(<em>request</em>, <em>template_name='500.html'</em>)<a class="headerlink" href="#django.views.defaults.server_error" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Similarly, Django executes special-case behavior in the case of runtime errors
in view code. If a view results in an exception, Django will, by default, call
the view <code class="docutils literal notranslate"><span class="pre">django.views.defaults.server_error</span></code>, which either produces a very
simple “Server Error” message or loads and renders the template <code class="docutils literal notranslate"><span class="pre">500.html</span></code> if
you created it in your root template directory.</p>
<p>The default 500 view passes no variables to the <code class="docutils literal notranslate"><span class="pre">500.html</span></code> template and is
rendered with an empty <code class="docutils literal notranslate"><span class="pre">Context</span></code> to lessen the chance of additional errors.</p>
<p>If <a class="reference internal" href="settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> is set to <code class="docutils literal notranslate"><span class="pre">True</span></code> (in your settings module), then
your 500 view will never be used, and the traceback will be displayed
instead, with some debug information.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.10:</span> <p>Passing a nonexistent <code class="docutils literal notranslate"><span class="pre">template_name</span></code> will raise <code class="docutils literal notranslate"><span class="pre">TemplateDoesNotExist</span></code>.</p>
</div>
</div>
<div class="section" id="s-the-403-http-forbidden-view">
<span id="s-http-forbidden-view"></span><span id="the-403-http-forbidden-view"></span><span id="http-forbidden-view"></span><h3>The 403 (HTTP Forbidden) view<a class="headerlink" href="#the-403-http-forbidden-view" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="django.views.defaults.permission_denied">
<code class="descclassname">defaults.</code><code class="descname">permission_denied</code>(<em>request</em>, <em>exception</em>, <em>template_name='403.html'</em>)<a class="headerlink" href="#django.views.defaults.permission_denied" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>In the same vein as the 404 and 500 views, Django has a view to handle 403
Forbidden errors. If a view results in a 403 exception then Django will, by
default, call the view <code class="docutils literal notranslate"><span class="pre">django.views.defaults.permission_denied</span></code>.</p>
<p>This view loads and renders the template <code class="docutils literal notranslate"><span class="pre">403.html</span></code> in your root template
directory, or if this file does not exist, instead serves the text
“403 Forbidden”, as per <span class="target" id="index-0"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc7231.html#section-6.5.3"><strong>RFC 7231#section-6.5.3</strong></a> (the HTTP 1.1 Specification).
The template context contains <code class="docutils literal notranslate"><span class="pre">exception</span></code>, which is the unicode
representation of the exception that triggered the view.</p>
<p><code class="docutils literal notranslate"><span class="pre">django.views.defaults.permission_denied</span></code> is triggered by a
<a class="reference internal" href="exceptions.html#django.core.exceptions.PermissionDenied" title="django.core.exceptions.PermissionDenied"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PermissionDenied</span></code></a> exception. To deny access in a
view you can use code like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="k">import</span> <span class="n">PermissionDenied</span>

<span class="k">def</span> <span class="nf">edit</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">pk</span><span class="p">):</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_staff</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">PermissionDenied</span>
    <span class="c1"># ...</span>
</pre></div>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 1.10:</span> <p>Passing a nonexistent <code class="docutils literal notranslate"><span class="pre">template_name</span></code> will raise <code class="docutils literal notranslate"><span class="pre">TemplateDoesNotExist</span></code>.</p>
</div>
</div>
<div class="section" id="s-the-400-bad-request-view">
<span id="s-http-bad-request-view"></span><span id="the-400-bad-request-view"></span><span id="http-bad-request-view"></span><h3>The 400 (bad request) view<a class="headerlink" href="#the-400-bad-request-view" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="django.views.defaults.bad_request">
<code class="descclassname">defaults.</code><code class="descname">bad_request</code>(<em>request</em>, <em>exception</em>, <em>template_name='400.html'</em>)<a class="headerlink" href="#django.views.defaults.bad_request" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>When a <a class="reference internal" href="exceptions.html#django.core.exceptions.SuspiciousOperation" title="django.core.exceptions.SuspiciousOperation"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SuspiciousOperation</span></code></a> is raised in Django,
it may be handled by a component of Django (for example resetting the session
data). If not specifically handled, Django will consider the current request a
‘bad request’ instead of a server error.</p>
<p><code class="docutils literal notranslate"><span class="pre">django.views.defaults.bad_request</span></code>, is otherwise very similar to the
<code class="docutils literal notranslate"><span class="pre">server_error</span></code> view, but returns with the status code 400 indicating that
the error condition was the result of a client operation. By default, nothing
related to the exception that triggered the view is passed to the template
context, as the exception message might contain sensitive information like
filesystem paths.</p>
<p><code class="docutils literal notranslate"><span class="pre">bad_request</span></code> views are also only used when <a class="reference internal" href="settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.10:</span> <p>Passing a nonexistent <code class="docutils literal notranslate"><span class="pre">template_name</span></code> will raise <code class="docutils literal notranslate"><span class="pre">TemplateDoesNotExist</span></code>.</p>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Built-in Views</a><ul>
<li><a class="reference internal" href="#serving-files-in-development">Serving files in development</a></li>
<li><a class="reference internal" href="#error-views">Error views</a><ul>
<li><a class="reference internal" href="#the-404-page-not-found-view">The 404 (page not found) view</a></li>
<li><a class="reference internal" href="#the-500-server-error-view">The 500 (server error) view</a></li>
<li><a class="reference internal" href="#the-403-http-forbidden-view">The 403 (HTTP Forbidden) view</a></li>
<li><a class="reference internal" href="#the-400-bad-request-view">The 400 (bad request) view</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="validators.html"
                        title="previous chapter">Validators</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="../misc/index.html"
                        title="next chapter">Meta-documentation and miscellany</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/ref/views.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="validators.html" title="Validators">previous</a>
     |
    <a href="index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="../misc/index.html" title="Meta-documentation and miscellany">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>